const folder = require('../models/model/folder');
const file = require('../models/model/file');

/**
 * 创建文件夹
 * @param {文件夹名} name 
 * @param {颜色} color 
 */
exports.createFolder = async (name = "", color = "", isDelete = false, userId = '') => {
    const ins = await folder.create({
        name,
        color,
        isDelete,
        userId
    });
    if (ins) {
        return ins.toJSON();
    }
    return null;
}


/**
 * 
 * @param {第几页} page 
 * @param {一页几条} limit 
 * @param {用户id} userId 
 * @returns 
 */
exports.findFolder = async (page = 1, limit = 100, userId = '') => {
    const ins = await folder.findAll({
        offset: (page - 1) * limit,
        limit: +limit,
        where: {
            userId
        }
    })
    if (ins) {
        return ins;
    }
    return null;
}

/**
 * 查询所有文件夹
 * @param {用户id}} userId 
 * @returns 
 */
exports.findFolderAllCount = async (userId) => {
    const ins = await folder.findAll({
        where: {
            userId
        }
    })
    if (ins) {
        return ins;
    }
    return null;
}

/**
 * 删除文件夹
 * @param {用户id} userId 
 * @param {文件夹id} folderId 
 * returns 返回删除的条数
 */
exports.deleteFolder = async (userId, folderId) => {
    const ins = await folder.destroy({
        where: {
            userId,
            id: folderId
        }
    });
    if (ins) {
        return ins;
    }
    return null;
}

/**
 * 修改文件夹的名字
 * @param {用户id} userId 
 * @param {文件夹id} folderId 
 * @param {文件夹名字} name 
 */
exports.editFolderName = async (userId, folderId, name) => {
    return await folder.update(
        {
            name,
        },
        {
            where: {
                userId,
                id: folderId
            }
        }
    );
}

/**
 * 通过文件夹id删除文件,主要用于删除文件夹的时候同时删除文件
 * @param {*} folderId 
 */
exports.deleteFileFolder = async (folderId) => {
    const ins = await file.destroy({
        where: {
            folderId: folderId
        }
    })
    if (ins) {
        return ins;
    }
    return null;
}